//
//  TagView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/7/2.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "TagView.h"

@interface TagView()

@property (nonatomic, strong) UIView *lastView;

@end

@implementation TagView

//- (void)awakeFromNib {
//
//    [super awakeFromNib];
//
//    self.autoresizesSubviews = NO;
//}

//- (void)setTagsArray:(NSArray *)tagsArray {
//
//    for (UIView * view in self.subviews) {
//        [view removeFromSuperview];
//        self.tagViewWidth.constant = 0.f;
//    }
//    NSInteger  total = tagsArray.count;
//    if (tagsArray.count > 2) {
//        total = 2;
//    }
//    for (int i = 0;i< total; i++) {
//        UILabel * label = [[UILabel alloc] init];
//        label.text = tagsArray[i];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.textColor = kThemeColor;
//        label.layer.cornerRadius = 7.5;
//        label.layer.borderWidth = 1.f;
//        label.font = Font(9);
//        label.layer.borderColor = kThemeColor.CGColor;
//        [self addSubview:label];
//
//        if (i == 0) {
//            label.frame = CGRectMake(0, 0, 40, 15);
//            self.tagViewWidth.constant = 45;
////            label.textColor = [UIColor whiteColor];
////            label.layer.backgroundColor = kThemeColor.CGColor;
//        }else if(i == 1){
//            label.frame = CGRectMake(48, 0, 40, 15);
//            self.tagViewWidth.constant = 93;
//        }
//    }
//}
- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        self.tagViewWidth.constant = 0;
    }
    return self;
}

- (void)setTagsArray:(NSArray *)tagsArray {
    
    if (tagsArray.count == 0) {
        self.tagViewWidth.constant = 0;
    }
    
    if ([_tagsArray isEqualToArray:tagsArray]) {
        
        return;
    }
    [self removeAllSubviews];
    self.lastView = nil;
    _tagsArray = tagsArray;
    
    CGFloat allWidth = 0;
    
    for (NSString *name in tagsArray) {
        
        CGFloat width = [IGUtil getSizeContent:name width:CGFLOAT_MAX font:Font(9)].width + 16;
        
        CGFloat x = allWidth == 0 ? 0 : (allWidth + 5);
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, width, 16)];
        label.text = name;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = kThemeColor;
        label.layer.cornerRadius = 8;
        label.layer.borderWidth = 1.f;
        label.font = Font(9);
        label.layer.borderColor = kThemeColor.CGColor;
        [self addSubview:label];

        if (self.lastView) {
            allWidth += (width + 5);
        } else {
            
            allWidth += width;
        }
        
        self.lastView = label;
    }
    
    self.tagViewWidth.constant = allWidth;
}
@end
